//
//  LRTabBarController.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/15.
//  Copyright © 2015年 leilurong. All rights reserved.
//

import UIKit

class LRTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //self.tabBar.tintColor = UIColor.orangeColor()
        let tabBar = LRTabBar()
        // 自定义tabbar,是只读的,不能直接使用 = 进行赋值
        // KVC 进行替换
        setValue(tabBar, forKey: "tabBar")
        
        tabBar.composeClosure = {() -> () in
            //如果登录过就跳转发微博控制器，否则授权界面
            if LRUserAccount.userLogin {
                let composeVc = LRComposeViewController(controllerType: LRComposeViewControllerType.Compose)
                self.presentViewController(UINavigationController(rootViewController: composeVc), animated: true, completion:nil)
            }else{
                let oAuthVc = LROAuthViewController()
                self.presentViewController(UINavigationController(rootViewController: oAuthVc), animated: true, completion: nil)
            }
        }
        
        
        let homeVC = LRHomeController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        let discoverVc = LRDiscoverController()
        addChildViewController(discoverVc, title: "发现", imageName: "tabbar_discover")
        
        let messageVc = LRMessageController()
        addChildViewController(messageVc, title: "信息", imageName: "tabbar_message_center")
        
        let profileVc = LRProfileController()
        addChildViewController(profileVc, title: "我", imageName: "tabbar_profile")
    }
    //私有方法private
    private func addChildViewController(controller: UIViewController,title: String,imageName:String) {
        //创建导航控制器包装控制器
        let nav = LRNavigationController(rootViewController: controller)
        //将nav添加给UITabBarController
        self.addChildViewController(nav)
        //这个可以统一设置标题和图片选中状态颜色（如果图片和字体颜色要求一致可以直接使用这方法设置）
        //self.tabBar.tintColor = UIColor.orangeColor()
        //nav.tabBarItem.title = title;
        controller.title = title;
        //设置选中标题属性，系统默认渲染成蓝色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        //设置选中状态图片属性---AlwaysOriginal保持原图状态，系统默认渲染成蓝色
        controller.tabBarItem.image = UIImage(named: imageName)
        let selectImageName = imageName + "_highlighted"
        controller.tabBarItem.selectedImage = UIImage(named: selectImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
        
    }
}
